package com.example.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.reggie.mapper.CategoryMapper;
import com.example.reggie.entity.Category;
import com.example.reggie.entity.Dish;
import com.example.reggie.entity.Setmeal;
import com.example.reggie.service.CategoryService;
import com.example.reggie.service.DishService;
import com.example.reggie.service.SetmealService;
import com.example.reggie.utils.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    @Lazy
    private DishService dishService;
    @Autowired
    @Lazy
    private SetmealService setmealService;
    @Override
    public void remove(Long ids) {
        //查询分类有无绑定菜品
        LambdaQueryWrapper<Dish> dishLQW = new LambdaQueryWrapper<>();
        dishLQW.eq(Dish::getCategoryId,ids);
        int count1 = dishService.count(dishLQW);
        if (count1 > 0 ){
            throw new CustomException("当前分类已绑定菜品，不能删除");
        }
        //查询套餐有无绑定菜品
        LambdaQueryWrapper<Setmeal> setmealLQW = new LambdaQueryWrapper<>();
        setmealLQW.eq(Setmeal::getCategoryId,ids);
        int count2 = setmealService.count(setmealLQW);
        if (count2 > 0 ){
            throw new CustomException("当前套餐已绑定菜品，不能删除");
        }
        //都没有就正常删除
        removeById(ids);
    }
}
